﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chowpoo.Common;
using CourseSystem.Common;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;
using CourseSystem.Core.Enum;
using Webdiyer.WebControls.Mvc;

namespace CourseSystem.Web.Controllers
{
    [UserAuth(UserType = UserTypeEnum.Teacher)]
    public class AdminStudyController : BaseController
    {
        //
        // GET: /AdminStudy/

        public ActionResult Chapter()
        {
            return View();
        }

        public ActionResult ChapterListUc(int? pid)
        {
            CommonRequest.ClearCache();
            var list = ChapterBLL.GetList().OrderByDescending(o => o.OrderId);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }

        public ActionResult SectionListUc(int? pid, int cid)
        {
            CommonRequest.ClearCache();
            var list = SectionBLL.GetList(cid).OrderByDescending(o => o.OrderId);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }

        public ActionResult ChapterEditUc(int? id)
        {
            if (id.HasValue)
            {
                var info = ChapterBLL.Get(id.Value);
                if (info == null)
                    return View("NotFound");
                return PartialView(info);
            }
            return PartialView(new ChapterInfo { OrderId = (byte)(ChapterBLL.GetMax() + 1), Name = "" });
        }

        public ActionResult ChapterDel(int id)
        {
            if (ChapterBLL.Del(id))
            {
                TempData["AlertMsg"] = "删除成功.";
            }
            return RedirectToAction("Chapter");
        }

        public ActionResult SectionDel(int id, int cid)
        {
            var info = SectionBLL.Get(id);
            if (info == null)
                return NotFound();
            var toDelList = info.CommonContentInfo.AttachmentsInfo.ToList();
            if (SectionBLL.Del(id))
            {
                AttachmentsBLL.DelFileByList(toDelList);
                SetAlertMsg("删除成功.", true);
            }
            return RedirectToAction("Section", new { cid });
        }

        [HttpPost]
        public ActionResult ChapterEditUc(ChapterInfo info)
        {
            ViewData.Model = info;
            if (!ModelState.IsValid)
                return PartialView();
            if (info.Id != 0)
            {
                var oldInfo = ChapterBLL.Get(info.Id);
                if (oldInfo == null)
                    return View("NotFound");
                oldInfo.Name = info.Name;
                oldInfo.OrderId = info.OrderId;
                if (ChapterBLL.Edit(oldInfo))
                {
                    ViewData["Msg"] = "编辑成功.";
                    return PartialView(oldInfo);
                }
                ViewData["Msg"] = "未知错误.";
                return PartialView();
            }
            if (ChapterBLL.Add(info))
            {
                ViewData["Msg"] = "编辑成功.";
                return PartialView(info);
            }
            ViewData["Msg"] = "未知错误.";
            return PartialView();
        }

        public ActionResult Section(int? cid)
        {
            if (!cid.HasValue)
                return View("NotFound");
            var info = ChapterBLL.Get(cid.Value);
            if (info == null)
                return View("NotFound");
            return View(info);
        }

        public ActionResult SectionEdit(int? id, int? cid, string ran)
        {
            ViewData["ranStr"] = ran;
            if (string.IsNullOrEmpty(ran))
            {
                var ranStr = RandomStr.GetByTime(UserHelper.LoginUser.Id + "_" + (byte)UserHelper.LoginUser.UserType + "_");
                ViewData["ranStr"] = ranStr;
                if (string.IsNullOrEmpty(ranStr))
                    return View("NotFound");
            }
            else
            {
                if (!RandomStr.Check(ran))
                    return View("NotFound");
            }
            if (id.HasValue)
            {
                var info = SectionBLL.Get(id.Value);
                if (info == null)
                    return View("NotFound");
                return View(info);
            }
            if (cid.HasValue)
                return View(new SectionInfo { ChapterId = cid.Value, OrderId = (byte)(SectionBLL.GetMax(cid.Value) + 1) });
            return View("NotFound");
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult SectionEdit(SectionInfo info, string ran, int cid)
        {
            ViewData["ranStr"] = ran;
            info.ChapterId = cid;
            if (!RandomStr.Check(ran))
                return View("NotFound");
            ViewData.Model = info;
            if (!ModelState.IsValid)
            {
                return View();
            }
            if (info.Id != 0)
            {
                var oldInfo = SectionBLL.Get(info.Id);
                if (oldInfo == null)
                    return View("NotFound");
                oldInfo.Name = info.Name;
                oldInfo.CommonContentInfo.DetailContent = (info.CommonContentInfo.DetailContent ?? "");
                if (CommonContentBLL.Edit(oldInfo.CommonContentInfo) && SectionBLL.Edit(oldInfo))
                {
                    //RandomStr.Remove(ran);
                    AttachmentsHelper.ClearUselessAttach(oldInfo.CommonContentInfo.DetailContent, ran, oldInfo.ContentId);
                    ViewData["AlertMsg"] = "编辑成功.";
                    return View(oldInfo);
                }
                ViewData["AlertMsg"] = "未知错误.";
                return View();
            }
            if (cid == 0)
            {
                ViewData["AlertMsg"] = "参数丢失,请重新添加.";
                return View();
            }
            info.CommonContentInfo.DetailContent = info.CommonContentInfo.DetailContent ?? "";
            info.ChapterId = cid;
            if (SectionBLL.Add(info))
            {
                // RandomStr.Remove(ran);
                AttachmentsHelper.ClearUselessAttach(info.CommonContentInfo.DetailContent, ran, info.ContentId);
                TempData["AlertMsg"] = "添加成功.";
                return RedirectToAction("SectionEdit", new { id = info.Id, ran = ran });
            }
            ViewData["AlertMsg"] = "未知错误.";
            return View();
        }

        public ActionResult StudyEdit(int cid, byte ty, string ran)
        {
            ViewData["ranStr"] = ran;
            if (string.IsNullOrEmpty(ran))
            {
                var ranStr = RandomStr.GetByTime(UserHelper.LoginUser.Id + "_" + (byte)UserHelper.LoginUser.UserType + "_");
                ViewData["ranStr"] = ranStr;
                if (string.IsNullOrEmpty(ranStr))
                    return View("NotFound");
            }
            else
            {
                if (!RandomStr.Check(ran))
                    return View("NotFound");
            }
            //检验合法性
            var test = (CourseStudyMenuEnum)ty;
            var sectionInfo = SectionBLL.Get(cid);
            if (sectionInfo == null)
            {
                return View("NotFound");
            }
            var info = CourseStudyBLL.Get(cid, ty);
            if (info == null)
            {
                info = new CourseStudyInfo
                                  {
                                      SectionId = cid,
                                      Type = ty,
                                      CommonContentInfo = new CommonContentInfo { DetailContent = "" }
                                  };
                if (!CourseStudyBLL.Add(info))
                {
                    return View("NotFound");
                }
            }
            return View(info);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult StudyEdit(CourseStudyInfo info, string ran)
        {
            ViewData["ranStr"] = ran;
            if (!RandomStr.Check(ran))
                return View("NotFound");
            ViewData.Model = info;
            if (!ModelState.IsValid)
                return View();
            if (info.Id != 0)
            {
                var oldInfo = CourseStudyBLL.Get(info.Id);
                if (oldInfo == null)
                    return View("NotFound");
                oldInfo.CommonContentInfo.DetailContent = (info.CommonContentInfo.DetailContent ?? "");
                if (CommonContentBLL.Edit(oldInfo.CommonContentInfo))
                {
                    //RandomStr.Remove(ran);
                    AttachmentsHelper.ClearUselessAttach(oldInfo.CommonContentInfo.DetailContent, ran, oldInfo.ContentId);
                    ViewData["AlertMsg"] = "编辑成功.";
                    return View(oldInfo);
                }
                SetAlertMsg("未知错误.");
                return View();
            }
            return View("NotFound");
        }

        public ActionResult Internet()
        {
            return View();
        }

        public ActionResult InternetListUc(int? pid)
        {
            CommonRequest.ClearCache();
            var list = InternetResourcesBLL.GetList().OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }

        public ActionResult InternetEditUc(int? id)
        {
            if (id.HasValue)
            {
                var info = InternetResourcesBLL.Get(id.Value);
                if (info == null)
                    return NotFound();
                return PartialView(info);
            }
            return PartialView(new InternetResourcesInfo { });
        }

        [HttpPost]
        public ActionResult InternetEditUc(InternetResourcesInfo info)
        {
            ViewData.Model = info;
            if (!ModelState.IsValid)
                return PartialView();
            if (info.Id != 0)
            {
                var oldInfo = InternetResourcesBLL.Get(info.Id);
                if (oldInfo == null)
                    return NotFound();
                oldInfo.Name = info.Name;
                oldInfo.Link = info.Link;
                if (InternetResourcesBLL.Edit(oldInfo))
                {
                    SetHighLineMsg("编辑成功.");
                    return PartialView(oldInfo);
                }
                SetHighLineMsg("未知错误.");
                return PartialView();
            }
            if (InternetResourcesBLL.Add(info))
            {

                SetHighLineMsg("编辑成功.");
                return PartialView();
            }
            SetHighLineMsg("未知错误.");
            return PartialView();
        }

        public ActionResult InternetDel(int id)
        {
            if (InternetResourcesBLL.Del(id))
                SetAlertMsg("删除成功.");
            return RedirectToAction("Internet");
        }

    }
}
